$(function () {
    axiosEach()
});

function eachCreate(doms,option) {
    var myChart = echarts.init(document.getElementById(doms));
    myChart.setOption(option);
}

/**
 *
 * @param triggerValue 点击次数
 * @param categoryValue 下载统计
 * @param UserUploadClickXvalue 用户上传x轴
 * @param UserUploadClickValue 用户上传数据
 */
function create(triggerValue,categoryValue,UserUploadClickXvalue,UserUploadClickValue) {
    //扇形
    let trigger = {
        tooltip: {
            trigger: 'item'
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 10,
            top: 20,
            bottom: 20,
        },
        series: [
            {
                name: '点击次数',
                type: 'pie',
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 40,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: triggerValue
            }
        ]
    }
    eachCreate('clicks',trigger)

    //柱状下载
    let category = option = option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'value',
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'category',
                data: ['压缩包类型', '文档类型', '视频类型', '图片类型', '音频类型'],
            }
        ],
        series: [
            {
                name: '下载次数',
                type: 'bar',
                barWidth: '60%',
                // data: [10, 20, 10, 14, 30, 30, 20]
                data:categoryValue
            }
        ]
    };
    eachCreate('DownloadStatistics',category)

    //柱状上传
    let UserUploadClick = option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            data: UserUploadClickXvalue
            // data: UserUploadClickXvalue
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: UserUploadClickValue,
                // data:UserUploadClickValue
                type: 'bar',
                barWidth: 30 // 这里设置了柱子的宽度为30px
            }
        ]
    };
    eachCreate('UserUpload',UserUploadClick)
}

function axiosEach() {
    $.ajax({
        url: '/chapterDocument/getConceptType',
        type: 'get',
        success: function(response) {
            // 请求成功时的处理
            if(response.code !== '0') {
                layer.msg(response.msg);
                return
            }

            dataCreate(response.data.data)
        },
        error: function(xhr, status, error) {
            // 请求失败时的处理
            layer.msg(error);
        }
    });
}

function dataCreate(data) {
    console.log(data)
    //数据概览
    let { groupByCourse} = data
    let total = 0
    //下载统计
    let {download} =data
    let downValue = []
    //点击次数
    let {tap} =data
    let tapList =[]

    //用户上传
    let { groupByUser } = data
    let xValue =[]
    let userValue = []

    //数据概览
    for (const key in groupByCourse) {
        total+= groupByCourse[key] * 1
    }
    doms('资源总数量',total)
    for (const key in groupByCourse) {
        switch (key) {
            case '1':
                doms('压缩包类型',groupByCourse[key])
                break;
            case '2':
                doms('文档类型',groupByCourse[key])
                break;
            case '3':
                doms('视频类型',groupByCourse[key])
                break;
            case '4':
                doms('图片类型',groupByCourse[key])
                break;
            case '5':
                doms('音频类型',groupByCourse[key])
                break;
            default:
                break;
        }

    }

    //点击次数
    for (let i = 0; i < tap.length; i++) {
        let arr ={
            value: tap[i].frequency * 1,
            name: ''
        }
        switch (tap[i].type) {
            case '1':
                arr.name = '压缩包类型'
                break;
            case '2':
                arr.name = '文档类型'
                break;
            case '3':
                arr.name = '视频类型'
                break;
            case '4':
                arr.name = '图片类型'
                break;
            case '5':
                arr.name = '音频类型'
                break;
            default:
                break;
        }
        tapList.push(arr)
    }

    //下载统计
    for (let i = 0; i < download.length; i++) {
        downValue.push(download[i].frequency)
    }
    console.log(downValue)

    //用户上传
    for (const key in groupByUser) {
        xValue.push(key)
        userValue.push(groupByUser[key])
    }

    //图表加载
    create(tapList,downValue,xValue,userValue)

}

function doms(titles,value) {
    $('.titlesText').append(
        `
        <div class="headerTitle">
            <div style="margin-bottom: 20px">${titles}</div>
            <h1>${value}</h1>
        </div>
        `
    )
}